package com.zsrt.day20170412.student.client;


import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;

import com.zsrt.day20170412.student.dao.StudentDao;
import com.zsrt.day20170412.student.entity.StudentEntity;
import com.zsrt.utils.ArrayUtil;

/**
*@author jzh
*@创建时间 2017年4月12日上午11:46:09
*类说明：
*/
public class StudentClient {
	
	private StudentEntity[] students=new StudentEntity[5];
	private StudentDao dao=new StudentDao();
	private Scanner in=new Scanner(System.in);
	private Scanner in2=new Scanner(System.in);
	
	/**
	 * 用来初始化的方法
	 */
	@Before
	public void Init(){
		
		StudentEntity jmj=new StudentEntity();
		jmj.setName("贾梦菊");
		jmj.setScore(66);
		students[0]=jmj;
		StudentEntity zjy=new StudentEntity();
		zjy.setName("张嘉源");
		zjy.setScore(76);
		students[1]=zjy;
		StudentEntity lzh=new StudentEntity();
		lzh.setName("罗智壕");
		lzh.setScore(56);
		students[2]=lzh;
		StudentEntity hxq=new StudentEntity();
		hxq.setName("何小强");
		hxq.setScore(86);
		students[3]=hxq;
		StudentEntity jzh=new StudentEntity();
		jzh.setName("靳志辉");
		jzh.setScore(45);
		students[4]=jzh;
	}
	/**
	 * 查找不及格的测试类
	 */
	@Test
	public void findStudentScoerNGTest(){
		
		StudentEntity[] newStudent = dao.findStudentScoerNG(students);
		ArrayUtil.printArray(newStudent);
	}
	
	
	/**
	 * 更改成绩的测试类
	 */
	@Test
	public void updateStudentScoreTest(){
		System.out.println("输入要更改的学生姓名：");
		String name=in.nextLine();
		while(true){
			int index = dao.studentExistOrNotByName(students, name);
			if(index<0){
				System.out.println("名字不对，请重新输入：");
				name=in.nextLine();
			}else{
				break;
			}	
		}
		System.out.println("输入更改后的分数：");
		double score=in.nextDouble();
		StudentEntity student = dao.updateStudentScore(students, name, score);
		System.out.println(student);
	}
	
	/**
	 * 删除单个学生的测试类
	 */
	@Test
	public void deleteStudentByNameTest(){
		System.out.println("输入要删除的学生姓名：");
		String name = in.nextLine();
		while(true){
			int index=dao.studentExistOrNotByName(students, name);
			if(index<0){
				System.out.println("名字不对，请重新输入：");
				name=in.nextLine();
			}else{
				break;
			}
		}
		StudentEntity[] newStudents = dao.deleteStudentByName(students, name);
		System.out.println("删除后的信息是：");
		ArrayUtil.printArray(newStudents);
	}
	@Test
	public void deleteSomeStudentByNameTest(){
		//String[] names=new String[2];
		System.out.println("输入要删除的第1个学生姓名：");
		String name0 = in.nextLine();
		//names[0]=name0;
		System.out.println("输入要删除的第2个学生姓名：");
		String name1 = in.nextLine();
		//names[1]=name1;
		
		StudentEntity[] newStudents0 = dao.deleteStudentByName(students, name0);
		StudentEntity[] newStudents1 = dao.deleteStudentByName(newStudents0, name1);
		//StudentEntity[] newStudents = dao.deleteStudentByName(students, name1);
		System.out.println("删除后的信息是：");
		ArrayUtil.printArray(newStudents1);
	}
	/**
	 * 批量删除，指定删除个数的测试类
	 */
	@Test
	public void deleteSomeStudentByNameTest1(){
		System.out.println("输入要删除的学生数量：");		
		int n=in.nextInt();
		//in.close();
		String[] names=new String[n];
		for(int i=0;i<n;i++){
			System.out.println("输入要删除的第"+(i+1)+"个学生姓名：");
			names[i] = in2.nextLine();
		}
		StudentEntity[][] newStudents=new StudentEntity[n+1][];
		//初始化二维数组的前n个元素
		for(int i=0;i<n;i++){
			newStudents[n-i-1]=new StudentEntity[students.length-1-i];
		}
		//初始化二维数组的第n+1个元素
		newStudents[n]=students;
		//依次删除
		for(int i=0;i<n;i++){
			newStudents[n-1-i]=dao.deleteStudentByName(newStudents[n-i], names[i]);
		}
		
		//StudentEntity[] newStudents0 = dao.deleteStudentByName(students, name0);
		//StudentEntity[] newStudents1 = dao.deleteStudentByName(newStudents0, name1);
		//StudentEntity[] newStudents = dao.deleteStudentByName(students, name1);
		System.out.println("删除后的信息是：");
		ArrayUtil.printArray(newStudents[0]);
	}
	
	/**
	 * 增加单个学生的测试类
	 * @throws Exception
	 */
	@Test
	public void addStudentTest() throws Exception {
		System.out.println("输入要增加的学生姓名：");
		String name=in.nextLine();
		while(true){
			int index=dao.studentExistOrNotByName(students, name);
			if(index>=0){
				System.out.println("该学生已存在，请重新输入：");
				name=in.nextLine();
			}else if(name.length()>4||name.length()<2){
				System.out.println("名字不对，请重新输入：");
				name=in.nextLine();
			}else{
				break;
			}
		}
		System.out.println("请输入身份证号，简化为2位数字：");
		String idCard;
		while(true){
			idCard=in.nextLine();
			if(idCard.length()==2 && dao.isNumber(idCard)){
				break;
			}else{
				System.out.println("身份证号简化为2位数字，请重新输入：");
			}
		}
		System.out.println("请输入性别：");
		String sex;
		while(true){
			sex=in.nextLine();
			if(sex.equals("男")||sex.equals("女")){
				break;
			}else{
				System.out.println("性别不对，请重新输入：");
			}
		}
		System.out.println("请输入年龄：");
		int age;
		while(true){
			age=in.nextInt();
			if(age>=0&&age<=120){
				break;
			}else{
				System.out.println("年龄不对，请重新输入：");
			}
		}
		System.out.println("请输入分数：");
		double score;
		while(true){
			score=in.nextDouble();
			if(score>=0&&score<=100){
				break;
			}else{
				System.out.println("分数不对，请重新输入：");
			}
		}
		//StudentEntity[] newStudents=new StudentEntity[students.length+1];
		StudentEntity[] newStudents = dao.addStudent(students,name,idCard,score,age,sex);
		System.out.println("显示学生的信息：");
		ArrayUtil.printArray(newStudents);
		
	}
}
